package com.lhy.user.service.impl;




import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageHelper;
import com.lhy.domain.User;
import com.lhy.user.mapper.UserMapper;
import com.lhy.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCrypt;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
* @author lhy
* @description 针对表【user(用户表)】的数据库操作Service实现
* @createDate 2024-07-27 14:06:14
*/
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper iUserMapper;

    @Override
    public List<User> selectAll() {
        LambdaQueryWrapper<User> lqw = new LambdaQueryWrapper<>();
        lqw.eq(User::getIsDelete,0);
        return iUserMapper.selectList(lqw);
    }


    @Override
    public List<User> selectList(User iUser) {
        LambdaQueryWrapper <User> lqw = this.lqw(iUser);
        return iUserMapper.selectList(lqw);
    }

    @Override
    public Integer insert(User iUser) {
        LambdaQueryWrapper<User> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(User::getUsername,iUser.getUsername());
        List<User> users = iUserMapper.selectList(lambdaQueryWrapper);
        if (!users.isEmpty()) {
            return -1; // 存在相同的用户名
        }
        String hashpw = BCrypt.hashpw(iUser.getPassword(), BCrypt.gensalt());
        iUser.setPassword(hashpw);
        return iUserMapper.insert(iUser);
    }

    @Override
    @Transactional
    public Integer delete(String ids) {
        try {
            String[] split = ids.split(",");
            for (String id : split) {
                User iUser = new User();
                iUser.setId(Long.parseLong(id));
                iUser.setIsDelete(1);
                iUserMapper.updateById(iUser);
            }
            return 1;
        }catch (Exception e){
            return -1;
        }
    }

    @Override
    public Integer update(User iUser) {
        return iUserMapper.updateById(iUser);
    }

    @Override
    public Page<User> pageAll(Integer page, Integer size) {
        PageHelper.startPage(page,size);
        return (Page<User>)this.selectAll();
    }

    @Override
    public Page<User> pageList(User iUser, Integer page, Integer size) {
        PageHelper.startPage(page,size);
        return (Page<User>)this.selectList(iUser);
    }

    @Override
    public Integer status(Long id, Integer status) {
        User user = new User();
        user.setId(id);
        user.setStatus(status);
        return iUserMapper.updateById(user);
    }
    @Override
    @Transactional
    public void resetPwd(List<Long> ids) {
        for (Long id : ids) {
            String hashpw = BCrypt.hashpw("123456", BCrypt.gensalt());
            User user = new User();
            user.setId(id);
            user.setPassword(hashpw);
            int i = iUserMapper.updateById(user);
            if (i != 1) {
                throw new RuntimeException("重置密码失败");
            }
        }
    }
    @Override
    public Integer login(String username, String password) {
        LambdaQueryWrapper<User> lqw = new LambdaQueryWrapper<>();
        lqw.eq(User::getUsername, username);
        lqw.eq(User::getIsDelete, 0);
        User user = iUserMapper.selectOne(lqw);
        if (user == null) {
            return -2;
        }
        if (user.getStatus() == 1) {
            return -3;
        }
        if (!BCrypt.checkpw(password, user.getPassword())) {
            return -1;
        }


        return 1;
    }



    public LambdaQueryWrapper<User> lqw(User iUser){
        LambdaQueryWrapper <User> lqw = new LambdaQueryWrapper<>();
        if (iUser.getId() != null){
            lqw.eq(User::getId,iUser.getId());
        }
        if (iUser.getUsername() != null){
            lqw.eq(User::getUsername,iUser.getUsername());
        }
        if (iUser.getAvatar() != null){
            lqw.eq(User::getAvatar,iUser.getAvatar());
        }
        if (iUser.getPassword() != null){
            lqw.eq(User::getPassword,iUser.getPassword());
        }
        if (iUser.getPhoneNum() != null){
            lqw.eq(User::getPhoneNum,iUser.getPhoneNum());
        }
        if (iUser.getEmail() != null){
            lqw.eq(User::getEmail,iUser.getEmail());
        }
        if (iUser.getNickname() != null){
            lqw.eq(User::getNickname,iUser.getNickname());
        }
        if (iUser.getDepartment() != null){
            lqw.eq(User::getDepartment,iUser.getDepartment());
        }
        if (iUser.getRole() != null){
            lqw.eq(User::getRole,iUser.getRole());
        }
        if (iUser.getStatus() != null){
            lqw.eq(User::getStatus,iUser.getStatus());
        }
        if (iUser.getIsDelete() != null){
            lqw.eq(User::getIsDelete,iUser.getIsDelete());
        }
        if (iUser.getCreateTime() != null){
            lqw.eq(User::getCreateTime,iUser.getCreateTime());
        }
        if (iUser.getUpdateTime() != null){
            lqw.eq(User::getUpdateTime,iUser.getUpdateTime());
        }
        return lqw;
    }
}




